/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PersonalPane.java
 *
 * Created on Jun 28, 2009, 4:18:35 PM
 */
package org.dbdir.client.gui;

import javax.swing.JOptionPane;
import org.dbdir.Client;

/**
 *
 * @author thotheolh
 */
public class PersonalPane extends javax.swing.JPanel {

    Client client;
    MainFrame frame;

    /** Creates new form PersonalPane */
    public PersonalPane(Client client, MainFrame frame) {
        initComponents();
        setSize(326, 191);
        this.client = client;
        this.frame = frame;
    }

    public void init() {
        usrTF.setText(client.getUsername());
        usrTF.setEditable(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        contentPane = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        usrTF = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        rePwdTF = new javax.swing.JPasswordField();
        newPwdTF = new javax.swing.JPasswordField();
        oldPwdTF = new javax.swing.JPasswordField();
        bottomPane = new javax.swing.JPanel();
        rightPane = new javax.swing.JPanel();
        unregBtn = new javax.swing.JButton();
        cancelBtn = new javax.swing.JButton();
        okBtn = new javax.swing.JButton();
        leftPane = new javax.swing.JPanel();
        mainBtn = new javax.swing.JButton();

        setLayout(new java.awt.BorderLayout());

        jLabel1.setText("Username: ");

        usrTF.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                usrTFActionPerformed(evt);
            }
        });

        jLabel2.setText("Old Password: ");

        jLabel3.setText("New Password: ");

        jLabel4.setText("Retype Password: ");

        javax.swing.GroupLayout contentPaneLayout = new javax.swing.GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(contentPaneLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(contentPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jLabel1)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(contentPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(oldPwdTF)
                    .addComponent(newPwdTF)
                    .addComponent(rePwdTF)
                    .addComponent(usrTF, javax.swing.GroupLayout.DEFAULT_SIZE, 172, Short.MAX_VALUE))
                .addContainerGap(14, Short.MAX_VALUE))
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(contentPaneLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(contentPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(usrTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(contentPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(oldPwdTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(contentPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(newPwdTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(contentPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(rePwdTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(14, Short.MAX_VALUE))
        );

        add(contentPane, java.awt.BorderLayout.CENTER);

        bottomPane.setLayout(new java.awt.BorderLayout());

        rightPane.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));

        unregBtn.setText("Unregister");
        unregBtn.setToolTipText("Unregister your user account");
        unregBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                unregBtnActionPerformed(evt);
            }
        });
        rightPane.add(unregBtn);

        cancelBtn.setText("Clear");
        cancelBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelBtnActionPerformed(evt);
            }
        });
        rightPane.add(cancelBtn);

        okBtn.setText("Update");
        okBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okBtnActionPerformed(evt);
            }
        });
        rightPane.add(okBtn);

        bottomPane.add(rightPane, java.awt.BorderLayout.EAST);

        leftPane.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        mainBtn.setText("Main");
        mainBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mainBtnActionPerformed(evt);
            }
        });
        leftPane.add(mainBtn);

        bottomPane.add(leftPane, java.awt.BorderLayout.CENTER);

        add(bottomPane, java.awt.BorderLayout.SOUTH);
    }// </editor-fold>//GEN-END:initComponents

    private void usrTFActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_usrTFActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_usrTFActionPerformed

    private void cancelBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelBtnActionPerformed
        oldPwdTF.setText("");
        newPwdTF.setText("");
        rePwdTF.setText("");
        oldPwdTF.grabFocus();
}//GEN-LAST:event_cancelBtnActionPerformed

    private void okBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okBtnActionPerformed
        if (newPwdTF.getText().equals(rePwdTF.getText())) {
            if (client.doUpdatePersonal(oldPwdTF.getText(), newPwdTF.getText())) {
                JOptionPane.showMessageDialog(null, "Your personal profile have been updated.", "Update Successful", JOptionPane.INFORMATION_MESSAGE);
                oldPwdTF.setText("");
                newPwdTF.setText("");
                rePwdTF.setText("");
                oldPwdTF.grabFocus();
            } else {
                JOptionPane.showMessageDialog(null, "Your personal profile have failed.", "Update Failed", JOptionPane.ERROR_MESSAGE);
                oldPwdTF.setText("");
                newPwdTF.setText("");
                rePwdTF.setText("");
                oldPwdTF.grabFocus();
            }
        } else {
            JOptionPane.showMessageDialog(null, "New Password and Re-type Password doesn't match !", "Password Don't Match", JOptionPane.WARNING_MESSAGE);
            newPwdTF.setText("");
            rePwdTF.setText("");
        }
    }//GEN-LAST:event_okBtnActionPerformed

    private void mainBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mainBtnActionPerformed
        // TODO add your handling code here:
        frame.getActionPane();
}//GEN-LAST:event_mainBtnActionPerformed

    private void unregBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_unregBtnActionPerformed
        // TODO add your handling code here:
        int n = JOptionPane.showConfirmDialog(
                null,
                "Are you sure you want to unregister your user account ?",
                "Unregister User Account",
                JOptionPane.YES_NO_OPTION);
        if (n == 0) {
            if (client.removeMe()) {
                JOptionPane.showMessageDialog(null,"Your user account have been successfully unregistered !","User Account Successfully Unregistered",JOptionPane.INFORMATION_MESSAGE);
                frame.getLoginPane();
            }else{
                JOptionPane.showMessageDialog(null,"Your user account have FAILED to be unregistered !","User Account Failed Unregistered",JOptionPane.ERROR_MESSAGE);
            }
        }
    }//GEN-LAST:event_unregBtnActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel bottomPane;
    private javax.swing.JButton cancelBtn;
    private javax.swing.JPanel contentPane;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel leftPane;
    private javax.swing.JButton mainBtn;
    private javax.swing.JPasswordField newPwdTF;
    private javax.swing.JButton okBtn;
    private javax.swing.JPasswordField oldPwdTF;
    private javax.swing.JPasswordField rePwdTF;
    private javax.swing.JPanel rightPane;
    private javax.swing.JButton unregBtn;
    private javax.swing.JTextField usrTF;
    // End of variables declaration//GEN-END:variables
}
